package net.sesami.nhg.ebd.batch.dao;

import java.util.Calendar;
import java.util.List;

import net.sesami.nhg.ebd.batch.constants.IConstants;
import net.sesami.nhg.ebd.batch.domain.hibernate.CO;
import net.sesami.nhg.ebd.batch.domain.hibernate.CO_Item;
import net.sesami.nhg.ebd.batch.domain.hibernate.PO;
import net.sesami.nhg.ebd.batch.domain.hibernate.POItem;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class CODAO {
	public List<CO> getAllCOReadyToSend() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String hqlQuery = String
				.format("select co from %s co where co.CO_Status_ID = :CO_Status_ID and co.Transmit_Status_ID = :Transmit_Status_ID",
						CO.class.getName());
		Query query = session.createQuery(hqlQuery);
		query.setInteger("CO_Status_ID", IConstants.CO_Status.APPROVED);
		query.setInteger("Transmit_Status_ID",
				IConstants.Transmit_Status.READY_TO_SEND);
		List<CO> cos = query.list();
		for (CO co : cos) {
			Hibernate.initialize(co.getPo());
			Hibernate.initialize(co.getPo().getPoItems());
			Hibernate.initialize(co.getCo_Items());
			Hibernate.initialize(co.getStatic_Payment());
			Hibernate.initialize(co.getStatic_Bill_To());
			Hibernate.initialize(co.getStatic_Ship_To());
			Hibernate.initialize(co.getStatic_Supplier());
			Hibernate.initialize(co.getStatic_Supplier().getSupplier());
			Hibernate.initialize(co.getStatic_Supplier().getShipMethod());
			Hibernate.initialize(co.getCreator());
			Hibernate.initialize(co.getUpdated_By());
			Hibernate.initialize(co.getCreator_Borg());
			Hibernate.initialize(co.getPo().getBorg().getBillTo());
			Hibernate.initialize(co.getPo().getBorg().getBillTo().getAddress());
			Hibernate.initialize(co.getPo().getBorg().getBillTo().getContact());
			Hibernate.initialize(co.getCo_Attachments());
			for (CO_Item co_Item : co.getCo_Items()) {
				Hibernate.initialize(co_Item.getCo_Item_Attachments());
			}
			for (POItem poItem : co.getPo().getPoItems()) {
				Hibernate.initialize(poItem.getPoCostDistribs());
				Hibernate.initialize(poItem.getPoItemAttachments());
			}
		}
		session.close();

		return cos;
	}

	public void updateCOAsWaitingForSupplierResponse(CO co) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction t = session.beginTransaction();
		String hqlQuery = String
				.format("update %s set CO_Status_ID = :CO_Status_ID, Transmit_Status_ID = :Transmit_Status_ID, Updated_Date = :Updated_Date where CO_ID = :CO_ID",
						CO.class.getName());
		Query query = session.createQuery(hqlQuery);
		query.setInteger("CO_Status_ID",
				IConstants.CO_Status.PENDING_SUPPLIER_RESPONSE);
		query.setInteger("Transmit_Status_ID",
				IConstants.Transmit_Status.SENT_TO_SUPPLIER);
		query.setDate("Updated_Date", Calendar.getInstance().getTime());
		query.setInteger("CO_ID", co.getCO_ID());
		query.executeUpdate();

		String hqlQueryPOUpdate = String.format(
				"update %s set POStatusID = :POStatusID where POID = :POID",
				PO.class.getName());
		Query query2 = session.createQuery(hqlQueryPOUpdate);
		query2.setInteger("POID", co.getPo().getPOID());
		query2.setInteger("POStatusID", IConstants.POStatus.CHANGE_PENDING);
		query2.executeUpdate();
		t.commit();
		session.close();
	}
}
